package cn.edu.jlu.community.app.sign;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.edu.jlu.community.R;
import cn.edu.jlu.community.app.base.AbstractActivity;

/**
 * Sign up / Sign in screen.
 */
public class SignActivity extends AbstractActivity {
    private FragmentManager mFragmentManager;
    private SignUpFragment mSignUpFragment;
    private SignInFragment mSignInFragment;

    @InjectView(R.id.layout_sign) protected View mRootView;
    @InjectView(R.id.logo) protected ImageView mLogo;

    public enum Mode {
        MODE_SIGN_UP,
        MODE_SIGN_IN,
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_activity);

        ButterKnife.inject(this);

        mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = mRootView.getRootView().getHeight() - mRootView.getHeight();
                if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                    mLogo.setVisibility(View.GONE);
                } else {
                    mLogo.setVisibility(View.VISIBLE);
                }
            }
        });

        mFragmentManager = getSupportFragmentManager();

        mSignUpFragment = (SignUpFragment) mFragmentManager
                .findFragmentById(R.id.fragment_sign_up);
        mSignInFragment = (SignInFragment) mFragmentManager
                .findFragmentById(R.id.fragment_sign_in);

        setMode(Mode.MODE_SIGN_IN);
    }
    public void setMode(Mode mode) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();

        hideFragments(transaction);

        switch (mode) {
            case MODE_SIGN_UP:
                transaction.show(mSignUpFragment);
                break;
            case MODE_SIGN_IN:
                transaction.show(mSignInFragment);
                break;
        }
        transaction.commit();
    }
    private void hideFragments(FragmentTransaction transaction) {
        if (mSignUpFragment != null) {
            transaction.hide(mSignUpFragment);
        }
        if (mSignInFragment != null) {
            transaction.hide(mSignInFragment);
        }
    }
}
